package org.jeecg.modules.demo.customer.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.customer.entity.YxCustomer;
import org.jeecg.modules.demo.customer.service.IYxCustomerService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: yx_customer
 * @Author: jeecg-boot
 * @Date:   2021-12-08
 * @Version: V1.0
 */
@Api(tags="yx_customer")
@RestController
@RequestMapping("/customer/yxCustomer")
@Slf4j
public class YxCustomerController extends JeecgController<YxCustomer, IYxCustomerService> {
	@Autowired
	private IYxCustomerService yxCustomerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param yxCustomer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "yx_customer-分页列表查询")
	@ApiOperation(value="yx_customer-分页列表查询", notes="yx_customer-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(YxCustomer yxCustomer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<YxCustomer> queryWrapper = QueryGenerator.initQueryWrapper(yxCustomer, req.getParameterMap());
			Page<YxCustomer> page = new Page<YxCustomer>(pageNo, pageSize);
			IPage<YxCustomer> pageList = yxCustomerService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(300,"网络故障！");
		}
	}

	@GetMapping(value = "/getCustomerList")
	public  Result<?> getCustomerList(){
		List<YxCustomer> yxCustomers = yxCustomerService.getCustomerList();
		return Result.ok(yxCustomers);
	}

	 @RequestMapping(value = "/revertLogicDeletedCustomer", method = RequestMethod.PUT)
	 public Result revertLogicDeletedCustomer(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
			 String customerIds = jsonObject.getString("customerIds");
			 if (StringUtils.isNotBlank(customerIds)) {
				 yxCustomerService.revertLogicDeletedCustomer(Arrays.asList(customerIds.split(",")));
			 }
			 return Result.ok("还原成功");

	 }
	/**
	 *   添加
	 *
	 * @param yxCustomer
	 * @return
	 */
	@AutoLog(value = "yx_customer-添加")
	@ApiOperation(value="yx_customer-添加", notes="yx_customer-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxCustomer yxCustomer) {
		try{
			yxCustomer.setDeadStatus(1);
			yxCustomer.setShanchu(0);
			yxCustomerService.save(yxCustomer);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}

	 /**
	  *   冻结
	  *
	  * @param yxCustomer
	  * @return
	  */
	 @AutoLog(value = "yx_customer-冻结")
	 @ApiOperation(value="yx_customer-冻结", notes="yx_customer-冻结")
	 @PostMapping(value = "/freeze")
	 public Result<?> freeze(@RequestBody YxCustomer yxCustomer) {
		 try {
			if(yxCustomer.getDeadStatus()==0){
				return Result.error(300,"已冻结,请勿重复冻结！");
			}
			yxCustomer.setDeadStatus(0);
			 yxCustomerService.updateById(yxCustomer);
			 return Result.OK("已冻结！");
		 }catch (Exception e){
		 	e.printStackTrace();
		 	return Result.error(500,"系统错误！");
		 }

	 }
	 /**
	  *   解冻
	  *
	  * @param yxCustomer
	  * @return
	  */
	 @AutoLog(value = "yx_customer-解冻")
	 @ApiOperation(value="yx_customer-解冻", notes="yx_customer-解冻")
	 @PostMapping(value = "/unfreeze")
	 public Result<?> unfreeze(@RequestBody YxCustomer yxCustomer) {
		 try {
			 if(yxCustomer.getDeadStatus()==1){
				 return Result.error(300,"已是解冻状态！");
			 }
			 yxCustomer.setDeadStatus(1);
			 yxCustomerService.updateById(yxCustomer);
			 return Result.OK("已解冻！");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }

	 }
	
	/**
	 *  编辑
	 *
	 * @param yxCustomer
	 * @return
	 */
	@AutoLog(value = "yx_customer-编辑")
	@ApiOperation(value="yx_customer-编辑", notes="yx_customer-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxCustomer yxCustomer) {
		try{
			if(yxCustomer.getDeadStatus()==0){
				return Result.error(300,"已冻结，请先解冻！");
			}
			yxCustomerService.updateById(yxCustomer);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "yx_customer-通过id删除")
	@ApiOperation(value="yx_customer-通过id删除", notes="yx_customer-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			yxCustomerService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "yx_customer-批量删除")
	@ApiOperation(value="yx_customer-批量删除", notes="yx_customer-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxCustomerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "yx_customer-通过id查询")
	@ApiOperation(value="yx_customer-通过id查询", notes="yx_customer-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxCustomer yxCustomer = yxCustomerService.getById(id);
		if(yxCustomer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxCustomer);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxCustomer
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxCustomer yxCustomer) {
        return super.exportXls(request, yxCustomer, YxCustomer.class, "yx_customer");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxCustomer.class);
    }

}
